//
//  KLMHomeItem.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/14.
//

import UIKit

enum HomeCellType {
    case normal
    case tap
    case detail
    case member
    case delete
}

enum HomeCellActionType {
    case none
    case push
    case createHome
    case joinHome
    case addMember
    case deleteMember
    case deleteHome
    case rename
    case leaveHome
    case cancelInvite
    case reInvite
    case popView
    case manageRoom
    case memberDetail
    case updateRoleType
}

struct KLMHomeItem {
    var type: HomeCellType
    var title: String? = ""
    var icon: String? = ""
    var titleColor : UIColor
    var subTitle: String? = ""
    var rightTitle : String? = ""
    var rightImage : String? = ""
    var rightTitleColor : UIColor
    var showRight: Bool = false
    var arrowIcon : String
    var showArrow: Bool = false
    var showLine: Bool = false
    var actionType: HomeCellActionType
    
    init(type: HomeCellType = .normal, title: String? = nil, icon: String? = nil, subTitle: String? = nil, titleColor: UIColor = rgba(38, 38, 38, 1), rightTitle: String? = nil, rightImage: String? = nil, rightTitleColor: UIColor = rgba(129, 129, 129, 1), showRight: Bool = false, arrowIcon: String = "icon_next", showArrow: Bool = false, showLine: Bool = false, actionType: HomeCellActionType = .none) {
        self.type = type
        self.title = title
        self.icon = icon
        self.subTitle = subTitle
        self.titleColor = titleColor
        self.rightTitle = rightTitle
        self.rightImage = rightImage
        self.rightTitleColor = rightTitleColor
        self.showRight = showRight
        self.arrowIcon = arrowIcon
        self.showArrow = showArrow
        self.showLine = showLine
        self.actionType = actionType
    }
    
    static func homeListItems() -> [[KLMHomeItem]] {
        return [
            [
                KLMHomeItem(type: .normal, title: "我的家...", showArrow: true),
            ],
            [
                KLMHomeItem(type: .tap, title: "创建家庭", titleColor: appMainThemeColor, actionType: .createHome)
            ],
            [
                KLMHomeItem(type: .tap, title: "加入一个家庭", titleColor: appMainThemeColor, actionType: .joinHome),
            ]
        ]
    }
    
    static func homeManageItems() -> [[KLMHomeItem]] {
        return [
            [
                KLMHomeItem(type: .normal, title: "家庭名称", rightTitle: "我的家..", showArrow: true),
                KLMHomeItem(type: .normal, title: "房间管理", rightTitle: "3", showArrow: true),
            ],
            [
                KLMHomeItem(type: .member, title: "86-18872006404", icon: "Icon_member",  subTitle: "86-18872006404", rightTitle: "管理者", showArrow: true),
                KLMHomeItem(type: .tap, title: "添加成员", titleColor: appMainThemeColor, actionType: .addMember),
            ],
            [
                KLMHomeItem(type: .delete, title: "删除家庭", titleColor: rgba(244, 57, 57, 1), actionType: .deleteHome)
            ],
        ]
    }
    /// 家庭设置 头部数据
    static func homeSettingHeaderItems(info: KLMHomeInfo, canEdit: Bool = false) -> [KLMHomeItem] {
        return [
            KLMHomeItem(type: .normal, title: "家庭名称", rightTitle: info.meshName, showArrow: canEdit, actionType: .rename),
            KLMHomeItem(type: .normal, title: "房间管理", rightTitle: "\(info.homeRoomCount)", showArrow: canEdit, actionType: .manageRoom),
        ]
    }
    /// 家庭设置 成员数据
    static func homeSettingMemberItems(users:[KLMHomeUser]) -> [KLMHomeItem] {
        return users.map { user in
            if user.userRoleType == .toInviteUser {
                /// 待加入的成员
                let (expired, _) = Date.getRemainingTime(timestamp: user.expiresTimestamp)
                if expired {
                    return KLMHomeItem(type: .member, title: user.homeNickname, icon: "Icon_member",  subTitle: user.showExpiresTime, showArrow: true, actionType: .memberDetail)
                } else {
                    return KLMHomeItem(type: .member, title: user.homeNickname, icon: "Icon_member",  subTitle: user.username, rightTitle: user.showExpiresTime, rightTitleColor: rgba(244, 57, 57, 1), showArrow: true, actionType: .memberDetail)
                }
            } else {
                /// 已加入的成员
                let subTitle = (user.telephone != nil && !user.telephone!.isEmpty) ? "86-\(user.telephone!)" : "\(user.email ?? "")"
                let title = user.homeNickname.isEmpty ? user.username : user.homeNickname
                return KLMHomeItem(type: .member, title: title, icon: "Icon_member",  subTitle: subTitle, rightTitle: user.userTypeString, showArrow: true, actionType: .memberDetail)
            }
        }
    }
    /// 家庭设置 尾部数据
    static func homeSettingFooterItems(currentUserType: HomeMemberRoleType) -> [KLMHomeItem] {
        if currentUserType == .Owner {
            return [KLMHomeItem(type: .delete, title: "删除家庭", titleColor: rgba(244, 57, 57, 1), actionType: .deleteHome)]
        } else {
            return [KLMHomeItem(type: .delete, title: "离开家庭", titleColor: rgba(244, 57, 57, 1), actionType: .leaveHome)]
        }
    }
    
    /// 家庭成员详情
    static func homeMemberDetailItems(currentUser: KLMHomeUser, lookUser: KLMHomeUser) -> [[KLMHomeItem]] {
        
        var items: [[KLMHomeItem]] = []
        
        let account = (lookUser.telephone != nil && !lookUser.telephone!.isEmpty) ? "86-\(lookUser.telephone!)" : "\(lookUser.email ?? "")"
        let rightTitle = lookUser.userRoleType == .toInviteUser ? lookUser.username : account
        
        let title = lookUser.homeNickname.isEmpty ? lookUser.username : lookUser.homeNickname
        var nameItem = KLMHomeItem(type: .normal, title: "名称", rightTitle: title, showArrow: false, actionType: .rename)
        let iconItem = KLMHomeItem(type: .normal, title: "头像", rightImage: "Icon_member", showArrow: false, actionType: .none)
        
        let accountItem = KLMHomeItem(type: .normal, title: "关联账号", rightTitle: rightTitle, showArrow: false, actionType: .none)
        var roleItem = KLMHomeItem(type: .normal, title: "家庭角色", rightTitle: lookUser.userTypeString, showArrow: false, actionType: .updateRoleType)
        
        let reInviteItem = KLMHomeItem(type: .delete, title: "重新邀请", titleColor: rgba(244, 57, 57, 1), actionType: .reInvite)
        let cancelInviteItem = KLMHomeItem(type: .delete, title: "撤销邀请", titleColor: rgba(244, 57, 57, 1), showLine: true, actionType: .cancelInvite)
        
        switch currentUser.userRoleType {
        case .Owner:
            nameItem.showArrow = true
            roleItem.showArrow = currentUser.userId != lookUser.userId
            items = [
                [nameItem, iconItem],
                [accountItem, roleItem]
            ]
            if lookUser.userRoleType == .toInviteUser {
                items.append([reInviteItem, cancelInviteItem])
            } else if currentUser.userId != lookUser.userId {
                items.append([KLMHomeItem(type: .delete, title: "移除成员", titleColor: rgba(244, 57, 57, 1), actionType: .deleteMember)])
            }
        case .admin:
            if lookUser.userRoleType == .toInviteUser {
                nameItem.showArrow = true
                roleItem.showArrow = true
                items = [
                    [nameItem, iconItem],
                    [accountItem, roleItem]
                ]
                items.append([reInviteItem, cancelInviteItem])
            } else if lookUser.userRoleType == .Owner || lookUser.userRoleType == .admin {
                items = [
                    [nameItem, iconItem],
                    [accountItem, roleItem]
                ]
            } else if lookUser.userRoleType == .user {
                nameItem.showArrow = true
                roleItem.showArrow = true
                items = [
                    [nameItem, iconItem],
                    [accountItem, roleItem]
                ]
                items.append([KLMHomeItem(type: .delete, title: "移除成员", titleColor: rgba(244, 57, 57, 1), actionType: .deleteMember)])
            }
        default:
            nameItem.showArrow = currentUser.userId == lookUser.userId
            items = [
                [nameItem, iconItem],
                [accountItem, roleItem]
            ]
            break
        }
        
        return items
    }
}
